package com.ctrip.cap.internal.appium.test;

import org.testng.ITestResult;

import com.ctrip.cap.CapLogger;
import com.ctrip.cap.exception.ScreenshotSkipException;
import com.ctrip.cap.internal.domain.Context;
import com.ctrip.cap.internal.test.MethodFilter;

/**
 * 
 * @author ltyao
 *
 */
public class ExceptionHandler implements MethodFilter {

	@Override
	public void doFilter(Context context) {

		ITestResult testResult = context.getTestResult();
		Throwable throwable = testResult.getThrowable();

		if (throwable instanceof ScreenshotSkipException) {
			testResult.setStatus(ITestResult.SUCCESS);
			testResult.setThrowable(null);
			return;
		}

		if (ITestResult.FAILURE == testResult.getStatus()) {
			// LOGGER.warn("[[fk=Cap]]", throwable);
			// 与TestNG Logger冲突导致混乱
			logExceptionStep(throwable);
		}
		if (ITestResult.SKIP == testResult.getStatus()) {
			logExceptionStep(throwable);
		}

	}

	private void logExceptionStep(Throwable throwable) {
		String udid = CapLogger.capture();
		CapLogger.error("Error", throwable, udid);
	}
}
